package com.example.war;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.FloatMath;

public class Meteors {
    private Context mContext;
    private static final int COUNT = 3;
    private Quad mQuads[] = new Quad[COUNT];
    private Meteor[] mMeteors = new Meteor[COUNT];
    private Point mScrSize = new Point();
    private Point mBmpSize[] = new Point[2];

    public Meteors(Context context, GL10 gl) {
        mContext = context;

        Bitmap meteor1 = Utils.loadRawImage(mContext.getResources(), R.raw.meteor_1);
        mBmpSize[0] = new Point();
        mBmpSize[0].x = meteor1.getWidth();
        mBmpSize[0].y = meteor1.getHeight();

        Bitmap meteor2 = Utils.loadRawImage(mContext.getResources(), R.raw.meteor_2);
        mBmpSize[1] = new Point();
        mBmpSize[1].x = meteor2.getWidth();
        mBmpSize[1].y = meteor2.getHeight();

        for (int i = 0; i<COUNT; i++) {
        	mQuads[i] = new Quad(false);
        	//mQuads[i].setSize((int)(mBmpSize.x*(0.2+i*0.07)), (int)(mBmpSize.y*(0.2+i*0.07)));
        	if (i%2 == 0) {
        	    mQuads[i].setTexture(gl, meteor1, false);
        	} else {
        	    mQuads[i].setTexture(gl, meteor2, false);
        	}
            mMeteors[i] = new Meteor();
        }
        meteor1.recycle();
        meteor2.recycle();
        
        mScrSize = Utils.getScreenSize(context);
    }
    
    public void draw(GL10 gl) {
    	//mQuads[1].draw(gl, 0, 100, 150, 0, true);
    	long currentTime = System.currentTimeMillis();
    	Meteor meteor;
    	for (int i = 0; i < 3; i++) {
			meteor = mMeteors[i];
			if (!meteor.active) {
				initMeteor(gl, meteor, i);
			}
			int t = (int) (currentTime - meteor.startTime);
			if (t < 50)
				continue;
			t /= 50;
			float dist = meteor.v * t + meteor.a * t * t / 2;
			float radias = (float)Math.toRadians(meteor.angle);
			float distX = dist * FloatMath.cos(radias);
			float distY = dist * FloatMath.sin(radias);
			//Log.d("zzzz","v:"+meteor.v+" t:"+t+" a:"+meteor.a+"   distX:"+distX+" distY:"+distY);
			float xPos = meteor.position.x - distX;
			float yPos = meteor.position.y - distY;
			if (xPos < -100 || yPos < -100) {
				initMeteor(gl, meteor, i);
				continue;
			}
			gl.glPushMatrix();
			gl.glTranslatef(xPos, yPos, 0);
			gl.glRotatef(meteor.angle, 0, 0, 1);
    		//Log.d("zzzz","Meteor["+i+"]:"+xPos+", "+yPos);
    		meteor.quad.draw(gl, 0, 0, 0, 0, true);
    		gl.glPopMatrix();
    	}
    }
    
    private void initMeteor(GL10 gl, Meteor meteor, int seed) {
    	long currentTime = System.currentTimeMillis();
    	Random random = new Random(currentTime+seed);
    	meteor.startTime = currentTime + 1000 + random.nextInt(10) * 100;
    	//Log.d("zzzz","@ scrW:"+mScrSize.x+"  scrH:"+mScrSize.y);
    	
    	meteor.v = 25+ random.nextInt(15)*2;
    	meteor.a = 5+random.nextInt(5);
    	meteor.angle = 25+random.nextInt(15);
    	
    	// y = ax +b
    	float ascent = (float)Math.tan(Math.toRadians(150));
    	meteor.position.x =  mScrSize.x/2 + seed*80 + random.nextInt(60);
    	meteor.position.y = ascent * meteor.position.x + 800;
        //Log.d("zzzz","====== Init Pos:"+meteor.position.x+", "+meteor.position.y);

   	    meteor.active = true;
   	    int quadIndex = random.nextInt(COUNT);
    	meteor.quad = mQuads[quadIndex];
    	
    	int index = quadIndex%2;
    	float scale = 0.5f + random.nextInt(6) / 10f;
    	meteor.quad.setSize(mBmpSize[index].x*scale, mBmpSize[index].y*scale);
    }
    
    class Meteor {
        long startTime;
        PointF position = new PointF();
        PointF size = new PointF();
        float v;
        float a;
        float angle;
        boolean active;
        Quad quad;
    }
    
    public void clean(GL10 gl) {
        for (Quad quad:mQuads) {
            quad.deleteTexture(gl);
        }
    }
}
